package com.sinopec.basemodule.utils.kotlin

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.ReplacementSpan
import android.text.style.StyleSpan
import android.util.TypedValue
import android.widget.TextView
import com.sinopec.basemodule.utils.kotlin.annotation.Style
import com.sinopec.basemodule.utils.kotlin.bean.TextMoreStyle

/**
 * 类描述：textView扩展方法 <br/>
 * 创建人：lwj<br/>
 * 创建时间：2019/6/24 13:47 <br/>
 */
/*enum class FontStyleEnum {
    FONT_B,
    FONT_R,
    FONT_M
}

private val FONT_B = Typeface.createFromAsset(BaseApplication.getInstance().applicationContext.assets, "fonts/font_b.ttf")
        ?: throw IllegalArgumentException("检查字体保存路径是否有：fonts/font_b.ttf")

private val FONT_R = Typeface.createFromAsset(BaseApplication.getInstance().applicationContext.assets, "fonts/font_r.ttf")
        ?: throw IllegalArgumentException("检查字体保存路径是否有：fonts/font_r.ttf")

private val FONT_M = Typeface.createFromAsset(BaseApplication.getInstance().applicationContext.assets, "fonts/font_m.ttf")
        ?: throw IllegalArgumentException("检查字体保存路径是否有：fonts/font_m.ttf")*/

///**
// * 设置字体样式，默认不加粗
// */
//@JvmOverloads
//fun TextView?.setFontStyle(fontStyle: FontStyleEnum, isBold: Boolean = false) {
//    if (this == null) {
//        return
//    }
//    val typeface = getTypeface(fontStyle)
//    this.typeface = typeface
//    if (isBold) {
//        this.paint.isFakeBoldText = true
//    }
//}
//
//fun getTypeface(fontStyle: FontStyleEnum): Typeface {
//    return when (fontStyle) {
//        FontStyleEnum.FONT_B -> FONT_B
//        FontStyleEnum.FONT_R -> FONT_R
//        FontStyleEnum.FONT_M -> FONT_M
//    }
//}

/**
 * 设置字体样式
 */
fun TextView?.setMoreStyle(
        vararg itemArray: TextMoreStyle?) {
    if (this == null) {
        return
    }
    if (itemArray.isEmpty()) {
        this.text = ""
        return
    }
    val ssb = SpannableStringBuilder()
    itemArray.forEachIndexed { index, textBoldStyle ->
        textBoldStyle?.run {
            ssb.append(textStr)
            val startI = if (index == 0) {
                0
            } else {
                ssb.length - textStr.length
            }

            //设置颜色
            if (color != -1) {
                ssb.setSpan(
                        ForegroundColorSpan(color),
                        startI,
                        ssb.length,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                )
            }

            //设置粗细
            if (style != Style.NO_SET) {
                ssb.setSpan(
                        StyleSpan(style),
                        startI,
                        ssb.length,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                )
            }

            //设置字体大小
            if (textSize != -1) {
                ssb.setSpan(
                        AbsoluteSizeSpan(textSize),
                        startI,
                        ssb.length,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                )
            }

            //设置段落高度
            if (paragraphHeight != -1) {
                ssb.setSpan(
                        object : ReplacementSpan() {
                            override fun getSize(
                                    paint: Paint,
                                    text: CharSequence?,
                                    start: Int,
                                    end: Int,
                                    fm: Paint.FontMetricsInt?
                            ): Int {
                                if (fm != null) {
                                    //return后宽度为0，因此实际空隙和段落开始在同一行，需要加上一行的高度
                                    fm.top = -paragraphHeight - paint.getFontMetricsInt(fm)
                                    fm.ascent = fm.top
                                    fm.bottom = 0
                                    fm.descent = fm.bottom
                                }
                                return 0
                            }

                            override fun draw(
                                    canvas: Canvas,
                                    text: CharSequence?,
                                    start: Int,
                                    end: Int,
                                    x: Float,
                                    top: Int,
                                    y: Int,
                                    bottom: Int,
                                    paint: Paint
                            ) {
                            }
                        },
                        startI,
                        startI + 7,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                )
            }
        }
    }
    this.text = ssb
}


/**
 * 通过defaultFromStyle设置textView字体typeface
 */
fun TextView?.setDefaultFromStyle(typefaceType: Int = Typeface.BOLD) {
    if (this == null) {
        return
    }
    this.typeface = Typeface.defaultFromStyle(typefaceType)
}

/**
 * textView显示全部文字
 */
fun TextView?.setShowAllText() {
    if (this == null) {
        return
    }
    this.post {
        val layout = this.layout
        if (layout != null) {
            if (layout.getEllipsisCount(this.lineCount - 1) > 0) {
                //Log.d("123===", "Text is ellipsized");
                //当前size大小
                var textSize = this.textSize
                //测量字符串的长度
                val temPain: TextPaint = TextPaint(this.paint)
                temPain.textSize = textSize
                var measureWidth = temPain.measureText(this.text.toString())
                //得到TextView 的宽度
                val width = this.width - this.paddingLeft - this.paddingRight
                while (width < measureWidth) {
                    textSize *= width / measureWidth
                    temPain.textSize = textSize
                    measureWidth = temPain.measureText(this.text.toString())
                }

                val minSize = 11.dp2px()
                if (textSize < minSize) {
                    //注意，使用像素大小设置
                    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, minSize.toFloat())
                } else {
                    //注意，使用像素大小设置
                    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
                }
            }/* else {
                     //Log.d("123===", "Text is not ellipsized");
                }*/
        }
    }
}